<template>
  <div class="app-container calendar-list-container">
    <!-- filter 条件查询 -->
    <div class="filter-container">
      <el-form :inline="true">
        <div style="display:inline-block;font-size:15px;">医院名称：</div>
        <el-input
          @keyup.enter.native="handleFilter"
          clearable
          v-model="listQuery.hospitalName"
          style="width: 200px;margin-right:15px;margin-top:7px"
          class="filter-item"
          placeholder="请输入医院名称"
        ></el-input>
        <div style="display:inline-block;font-size:15px;">医院详细地址：</div>
        <el-input
          @keyup.enter.native="handleFilter"
          clearable
          v-model="listQuery.hospitalAddress"
          style="width: 200px;margin-right:15px;margin-top:7px"
          class="filter-item"
          placeholder="请输入医院详细地址"
        ></el-input>
        <div style="display:inline-block;font-size:15px;">科室：</div>
        <el-input
          @keyup.enter.native="handleFilter"
          clearable
          v-model="listQuery.departmentName"
          style="width: 200px;margin-right:15px;margin-top:7px"
          class="filter-item"
          placeholder="请输入科室"
        ></el-input>
        <el-button
          class="filter-item"
          type="primary"
          v-waves
          icon="el-icon-search"
          @click="handleFilter"
          style="margin-top:7px;"
        >搜索</el-button>
        <el-button
          type="primary"
          class="filter-item"
          @click="handleDownload"
          icon="el-icon-download"
          :loading="downloadLoading"
          style="margin-top:7px;"
        >批量导出</el-button>
        <el-button
          type="primary"
          v-waves
          icon="el-icon-circle-plus-outline"
          class="filter-item"
          @click="add1"
          style="margin-top:7px;"
        >新增</el-button>
      </el-form>
    </div>
    <!-- table 表格 -->
    <el-table
      :data="list"
      v-loading="loading.list"
      element-loading-text="给我一点时间"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="序号" prop="index" width="120px"/>
      <el-table-column align="center" label="医院名称" prop="hospitalName"/>
      <el-table-column align="center" label="医院详细地址" prop="hospitalAddress"/>
      <el-table-column align="center" label="科室" prop="departmentName"/>
      <el-table-column align="center" label="陪护床数量" prop="bedCount"/>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div class="fx">
            <el-button size="mini" type="danger" @click="del(scope.row.deptId)" v-if="scope.row.bedCount==0">删除</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="edit(scope.row)"
            >编辑</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.offset"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 添加科室和编辑科室弹框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialog.add" width="500px">
      <el-form
        :model="addQuery"
        :rules="addRules"
        ref="addDept"
        label-position="right"
        label-width="120px"
      >
        <el-form-item label="医院" prop="deptName"> 
          <el-select v-model="addQuery.hospitalId"  placeholder="请选择" style="width:220px;" :disabled="dialogType !== 1">
            <el-option
              v-for="item in hospitalList"
              :key="item.hospitalId"
              :label="item.hospitalName"
              :value="item.hospitalId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室" prop="deptName">
          <el-input v-model="addQuery.deptName" placeholder="请输入科室" style="width:220px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loading.add" @click="setDate">确定</el-button>
        <el-button type="primary" @click="dialog.add = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { 
  getPartList, 
  setPart,
  dropDownHospitalList,
  delPart
} from "@/api/hospital";
import { parseTime } from "@/utils";
export default {
  name: "hospital_department",
  data() {
    return {
      total: 0,
      listQuery: {
        offset: 1,
        limit: 10,
        hospitalId: "",
        hospitalName: "",
        hospitalAddress: "",
        departmentName: "",
        wardName: "",
        bedName: "",
        equipmentNumber: "",
        deptName: "",
        type: 1
      },
      dialog: {
        add: false
      },
      loading: {
        list: false
      },
      list: null,
      dialogType: 1,
      addQuery: {
        hospitalId:"",
        deptName: "",
        deptType: 1,
        fid: 0
      },
      addRules: {
        deptName: [{ required: true, message: "请输入科室", trigger: "blur" }]
      },
      deptId: "",
      hospitalList:[],
      downloadLoading:false,
      hospitalId:''
    };
  },
  created() {
    this.getPartList();
  },
  computed: {
    dialogTitle() {
      return this.dialogType === 1 ? "新增科室" : "编辑科室";
    }
  },
  methods: {
    // 获取医院下拉列表
    async getHospiltalList(){
      const data = await dropDownHospitalList()
      if(data.code == 1){
        this.hospitalList = data.data
      }
    },
    //导出
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then(excel => {
        const tHeader = ["医院名称", "医院详细地址", "科室","陪护床数量"];
        const filterVal = ["hospitalName", "hospitalAddress", "departmentName","bedCount"];
        const data = this.formatJson(filterVal, this.list);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "科室列表"
        });
        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          return v[j];
        })
      );
    },
    //获取科室列表
    async getPartList() {
      this.loading.list = true;
      const { data } = await getPartList(this.listQuery);
      this.list = data.records.map((item, index) => ({
        index: index + 1,
        ...item
      }));
      this.total = data.total;
      this.loading.list = false;
    },
    //点击新增
    add1() {
      this.dialogType = 1;
      this.getHospiltalList().then(() => {
        this.dialog.add = true;
        this.addQuery = {
          deptName: "",
          deptType: 1,
          fid: 0
        };
      })
    },
    // 点击编辑
    edit({departmentName,deptId,hospitalName,hospitalId}) {
      this.dialog.add = true;
      this.dialogType = 2;
      this.addQuery.hospitalId = hospitalName
      this.addQuery.deptName = departmentName
      this.hospitalId = hospitalId
      this.deptId = deptId;
    },
    //点击删除
    async del(deptId) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const data = await delPart({ deptId });
          if(data.code == 1){
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getPartList();
          }
        })
        .catch(() => {});
    },
    //提交 新增和编辑
    async setDate() {
      this.$refs["addDept"].validate(async valid => {
        if (valid) {
          this.loading.add = true;
          const data = await setPart({
            deptType: 1,
            hospitalId: this.dialogType == 2 ? this.hospitalId : this.addQuery.hospitalId,
            deptName:this.addQuery.deptName,
            deptFid:this.dialogType == 2 ? this.hospitalId : '',
            deptId:this.dialogType == 2 ? this.deptId : ''
          });
          if (data.code == 1) {
            this.$message.success("操作成功");
            this.dialog.add = false;
            this.$refs["addDept"].resetFields();
            this.getPartList();
          }
          this.loading.add = false;
          this.addQuery.hospitalName = ''
        }
      });
    },
    handleFilter() {
      this.listQuery.offset = 1;
      this.getPartList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getPartList();
    },
    handleCurrentChange(val) {
      this.listQuery.offset = val;
      this.getPartList();
    }
  },
  onShow() {
  }
};
</script>
